# coding=utf-8
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.select import Select

# 路径根据自己的修改
browser = webdriver.Firefox(executable_path=".\exe\geckodriver.exe")
browser.get('https://www.nike.com/cn/t/air-vapormax-2020-fk-%E7%94%B7%E5%AD%90%E8%BF%90%E5%8A%A8%E9%9E%8B-F43qrX/CJ6740-001')
sleep(3)

# 模拟登陆的信息，要根据保存的cookie，下单就自动登录了，不用每次登录，这里是demo
browser.maximize_window()
# 将页面滚动条拖到底部
js = "var q=document.documentElement.scrollTop=10000"
browser.execute_script(js)

sleep(5)
# 点击登录按钮
browser.find_element_by_xpath("/html/body/div[1]/div[1]/div[2]/header/nav[1]/section[1]/div/div/ul[2]/li[1]/div/button").click()
browser.find_elements_by_name("verifyMobileNumber").pop(0).send_keys('15521248427')     # 账号，写自己的
browser.find_elements_by_name("password").pop(0).send_keys('Li651256347')   # 密码，写自己的
browser.find_elements_by_class_name("nike-unite-submit-button").pop(0).click()


sleep(10)
# 将页面滚动条拖到底部
js = "var q=document.documentElement.scrollTop=10000"
browser.execute_script(js)
# browser.execute_script("document.getElementsByClassName('hf-modal-window')[0].style.display = 'none';")
# -----------加购物车这一步，没有做；暂时先手动加购物车，在测试下面下单的
goods_size = "/html/body/div[1]/div[2]/div/div/div[3]/div[3]/div[2]/div/div/form/div[1]/fieldset/div/div[3]/label"
browser.find_element_by_xpath(goods_size).click()
cart_button = "/html/body/div[1]/div[2]/div/div/div[3]/div[3]/div[2]/div/div/form/div[2]/div/div/button[1]"
browser.find_element_by_xpath(cart_button).click()


sleep(5)  # 强制等待3秒再执行下一步
cookies = browser.get_cookies()
browser.delete_all_cookies()
for cookie in cookies:
    browser.add_cookie(cookie)
cookie2 = browser.get_cookies()     # 在查浏览器cookies，发现cookies值已经修改
print(cookie2)
browser.get("https://www.nike.com/cn/zh-Hans/checkout")

# 下单填地址信息
sleep(5)  # 强制等待3秒再执行下一步
lastName = browser.find_elements_by_id("lastName")
if len(lastName) > 0:
    lastName.pop(0).send_keys("黎")
firstName = browser.find_elements_by_id("firstName")
if len(firstName) > 0:
    firstName.pop(0).send_keys("科宁")

state = browser.find_elements_by_id("state")
if len(state) > 0:
    Select(state.pop(0)).select_by_value("CN-44")

city = browser.find_elements_by_id("city")
if len(city) > 0:
    Select(city.pop(0)).select_by_value("广州市")

county = browser.find_elements_by_id("county")
if len(county) > 0:
    Select(county.pop(0)).select_by_value("黄埔区")

address1 = browser.find_elements_by_id("address1")
if len(address1):
    address1.pop(0).send_keys("万达广场B001")

email = browser.find_elements_by_id("email")
if len(email) > 0:
    email.send_keys("likening24@sina.com")

phoneNumber = browser.find_elements_by_id("phoneNumber")
if len(phoneNumber):
    phoneNumber.pop(0).send_keys("15521248427")

js_next_step = browser.find_elements_by_class_name("js-next-step")
if len(js_next_step):
    js_next_step.pop(0).click()

sleep(3)  # 强制等待3秒再执行下一步
continuePaymentBtn = browser.find_elements_by_class_name("continuePaymentBtn")
if len(continuePaymentBtn):
    continuePaymentBtn.pop(0).click()

sleep(3)  # 强制等待3秒再执行下一步
browser.find_elements_by_class_name("ncss-btn-accent").pop(0).click()
